package org.example.managementsystem.controller;

import org.example.managementsystem.model.Production_Submission;
import org.example.managementsystem.service.Production_SubmissionService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/production_submissions")
@CrossOrigin(origins = "*")
public class Production_SubmissionController {
    private final Production_SubmissionService submissionService;

    public Production_SubmissionController(Production_SubmissionService submissionService) {
        this.submissionService = submissionService;
    }

    @GetMapping
    public ResponseEntity<List<Production_Submission>> getAllSubmissions() {
        List<Production_Submission> submissions = submissionService.getAllProduction_Submissions();
        return ResponseEntity.ok(submissions);
    }
    @GetMapping("/plan/{plan_id}")
    public ResponseEntity<List<Production_Submission>> getSubmissionsByPlanId(@PathVariable Long plan_id) {
        List<Production_Submission> submissions = submissionService.getProduction_SubmissionsByPlanId(plan_id);
        if (!submissions.isEmpty()) {
            return ResponseEntity.ok(submissions);
        }
        return ResponseEntity.noContent().build();
    }


    @GetMapping("/getAllApplicationUNPASS")
    public ResponseEntity<List<Production_Submission>> getAllApplicationUNPASS() {
        List<Production_Submission> submissions = submissionService.getAllApplicationUNPASS();
        return ResponseEntity.ok(submissions);
    }
    @GetMapping("/{submission_id}")
    public ResponseEntity<Production_Submission> getSubmissionById(@PathVariable int submission_id) {
        Production_Submission submission = submissionService.getProduction_SubmissionById(submission_id);
        if (submission != null) {
            return ResponseEntity.ok(submission);
        }
        return ResponseEntity.notFound().build();
    }

    @PostMapping
    public ResponseEntity<Map<String, Object>> addSubmission(@RequestBody Production_Submission submission) {
        Production_Submission savedSubmission = submissionService.addProduction_Submission(submission);
        Map<String, Object> response = new HashMap<>();
        response.put("submission_id", savedSubmission.getSubmission_id());
        response.put("message", "生产提交记录添加成功");
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    @PutMapping("/{submission_id}")
    public ResponseEntity<Map<String, String>> updateSubmission(
            @PathVariable int submission_id,
            @RequestBody Production_Submission submission) {
        submission.setSubmission_id(submission_id);
        boolean updated = submissionService.updateProduction_Submission(submission);

        Map<String, String> response = new HashMap<>();
        if (updated) {
            response.put("message", "生产提交记录更新成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "生产提交记录更新失败，未找到指定ID的记录");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    @DeleteMapping("/{submission_id}")
    public ResponseEntity<Map<String, String>> deleteSubmission(@PathVariable int submission_id) {
        boolean deleted = submissionService.deleteProduction_Submission(submission_id);

        Map<String, String> response = new HashMap<>();
        if (deleted) {
            response.put("message", "生产提交记录删除成功");
            return ResponseEntity.ok(response);
        } else {
            response.put("message", "生产提交记录删除失败，未找到指定ID的记录");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }
}